﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Serilog;
using TSNPro.Device;
using TSNPro.MyControl;
using TSNPro.MyForm;
using TSPro.MyControl;
using TSPro.MyForm;

namespace TSPro
{
    public partial class FormMain : Krypton.Toolkit.KryptonForm
    {
        public FormMain()
        {
            InitializeComponent();
            navMain.SelectedIndex = 0;

            Stopwatch sw = new Stopwatch();
            sw.Start();
            SSetControl sSetControl = new SSetControl();
            sSetControl.SetTitle("硫参数设置");
            controlPanel.Controls.Add(sSetControl);
            SSetControl nSetControl=new SSetControl();
            nSetControl.SetTitle("氮参数设置");
            controlPanel.Controls.Add(nSetControl);

            TSNCommonControl tSNCommonControl = new TSNCommonControl();
            tSNCommonControl.SetDevice(DeviceManager.GetInstance().GetItem<CommonDevice>("CommonDevice"));
            controlPanel.Controls.Add(tSNCommonControl);
            sw.Stop();
            Log.Information($"{sw.ElapsedMilliseconds}ms");
            pgbMeasure.ResetText();
        }



        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // 在窗体加载时保存图片
            //SaveImagesFromControls(controlPanel.Controls[1], @".\Images\TSNControl");
        }

        public void SaveImagesFromControls(Control control, string directory)
        {
            // 确保保存的目录存在
            Directory.CreateDirectory(directory);

            int index = 1;
            // 递归查找所有控件并保存其图片
            SaveImagesRecursive(control, directory, ref index);
        }

        private void SaveImagesRecursive(Control control, string directory, ref int index)
        {
            // 如果控件有 Image 属性并且该属性不为空，保存图片
            if (control is PictureBox pictureBox && pictureBox.Image != null)
            {
                SaveImage(pictureBox.Image, directory, ref index);
            }
            else if (control is Label label && label.Image != null)
            {
                SaveImage(label.Image, directory, ref index);
            }

            // 递归遍历所有子控件
            foreach (Control childControl in control.Controls)
            {
                SaveImagesRecursive(childControl, directory, ref index);
            }
        }

        private void SaveImage(Image image, string directory, ref int index)
        {
            // 定义图片保存路径
            string filePath = Path.Combine(directory, $"Image_{index}.png");

            // 保存图片
            image.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);

            // 更新索引
            index++;
        }

        private void tsmiDeviceSet_Click(object sender, EventArgs e)
        {

        }

        private void kryptonNumericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void tsmiQUeryStandard_Click(object sender, EventArgs e)
        {
            FormHistoryStandard frm= new FormHistoryStandard();
            frm.StartPosition = FormStartPosition.CenterParent;
            frm.Show();
        }

        private void tsmiDeviceConfig_Click(object sender, EventArgs e)
        {
            FormDeviceConfig frm= new FormDeviceConfig();
            frm.Show();
        }

        private void tsmiLog_Click(object sender, EventArgs e)
        {
            FormLog frm = FormLog.GetInstance();
            frm.Show();
        }
    }
}
